﻿
namespace GrpcSampleDbAccessClient
{
    using DbAccess.proto;
    using Grpc.Net.Client;
    using System;
    using System.Threading.Tasks;
    class Program
    {
        private static readonly GrpcChannel channel = GrpcChannel.ForAddress("https://localhost:5051");
        private static readonly DbAccessGrpc.DbAccessGrpcClient dbClient = new DbAccessGrpc.DbAccessGrpcClient(channel);
        static async Task Main()
        {
            var continueProgram = true;
            while (continueProgram)
            {
                Console.WriteLine(@"1- AddSample
2- AddRangeIEnumSample 
3- AddRangeParamT
4- GetListSample 
5- GetListAndDeleteSample 
6- PaginationSample 
7- SingleSample
8- SingleOrDefaultSample 
9- UpdateSample 
10- UpdateByPropertyNameSample 
11- DeleteSample 
12- DeleteByIDSample 
13- TransactionSample 
14- RollbackSample
15- CustomAddSample
16- AddSubjectSample
17- GetListSubjectSample
18- DeleteSubjectSample
19- DeleteByIDSubjectSample

Choose a number to execute the method");

                var input = Console.ReadLine();
                var dbReply = new ResultMessageResponse();
                var id = "";
                switch (input)
                {
                    case "1":
                        dbReply = await dbClient.AddSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "2":
                        dbReply = await dbClient.AddRangeIEnumSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "3":
                        dbReply = await dbClient.AddRangeParamTSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "4":
                        dbReply = await dbClient.GetListSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "5":
                        dbReply = await dbClient.GetListAndDeleteSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "6":
                        var paginationRequest = new PaginationRequest();
                        Console.WriteLine("Please input the pagination page index");
                        paginationRequest.PageIndex = Int32.Parse(Console.ReadLine());
                        Console.WriteLine("Please input the pagination page size");
                        paginationRequest.PageSize = Int32.Parse(Console.ReadLine());
                        dbReply = await dbClient.PaginationSampleAsync(paginationRequest);
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "7":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.SingleSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "8":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.SingleOrDefaultSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "9":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.UpdateSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "10":
                        var sampleRequest = new UpdatePropertyNameValueRequest();
                        Console.WriteLine("Please input the ID of the record");
                        sampleRequest.ID = Console.ReadLine();
                        Console.WriteLine("Please input the property Name");
                        sampleRequest.PropertyName = Console.ReadLine();
                        Console.WriteLine("Please input new property value");
                        sampleRequest.PropertyValue = Console.ReadLine();

                        dbReply = await dbClient.UpdateByPropertyNameSampleAsync(sampleRequest);
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "11":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.DeleteSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "12":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.DeleteByIDSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "13":
                        dbReply = await dbClient.TransactionSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "14":
                        dbReply = await dbClient.RollbackSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "15":
                        dbReply = await dbClient.CustomAddSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "16":
                        dbReply = await dbClient.AddSubjectSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "17":
                        dbReply = await dbClient.GetListSubjectSampleAsync(new NoParameterRequest());
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "18":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.DeleteSubjectSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    case "19":
                        Console.WriteLine("Please input the ID of the record");
                        id = Console.ReadLine();
                        dbReply = await dbClient.DeleteByIDSubjectSampleAsync(new SampleIDRequest { ID = id });
                        Console.WriteLine(dbReply.ResultMessage);
                        break;
                    default:
                        break;
                }
                Console.WriteLine("Do you want to try another method? [Y/N]");
                continueProgram = Console.ReadLine().ToUpper() == "Y";
            }

        }
    }
}
